package com.cee.admin.modules.cfd.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.admin.common.BaseController;
import com.cee.common.exception.GlobalException;
import com.cee.common.spring.SpringContextHolder;
import com.cee.common.utils.exportWord.WordTmpToNewWordUtils;
import com.cee.common.web.result.Result;
import com.cee.common.web.result.ResultCode;
import com.cee.domain.modules.cfd.vo.CfdCarbonNeutralityVo;
import com.cee.domain.modules.pro.vo.ProCarbonCreditVo;
import com.cee.domain.modules.sys.entity.SysFrontUser;
import com.cee.ds.common.utils.BasePrincipalUtils;
import com.cee.ds.modules.cfd.service.ICfdCarbonNeutralityService;
import com.cee.ds.modules.sys.service.ISysFrontUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产品碳中和表 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-04-08
 */
@Tag(name = "产品管理-碳足迹数据管理-碳中和")
@RestController
@RequestMapping("/api/cfd/carbon/neutrality")
public class CfdCarbonNeutralityController extends BaseController {

    @Autowired
    private ICfdCarbonNeutralityService iCfdCarbonNeutralityService;

    @Operation(summary = "分页")
    @GetMapping("/page")
    public Result<Page<CfdCarbonNeutralityVo>> page(@RequestParam Long current, @RequestParam Long size, CfdCarbonNeutralityVo query) {
        query.setCityId(BasePrincipalUtils.currentUser().getCityId());
        handleOrder(query);
        return success(iCfdCarbonNeutralityService.queryPageVo(new Page<>(current,size),query));
    }

    @Operation(summary = "查看报告证书")
    @GetMapping("/check/{id}")
    @RequiresPermissions("cfd:neutrality:certificate")
    public void check(@PathVariable("id") Long id) throws IOException {
        checkPermWithCity(id);
        WordTmpToNewWordUtils.changWord(ResourceUtil.getStream("wordTemplate/ProductCarbonNeutralityCertificate.docx"),wrapDownloadResponse("碳中和报告"+System.currentTimeMillis()+".docx"),iCfdCarbonNeutralityService.getReportInfo(id));
    }

    @Operation(summary = "查询碳信用列表")
    @GetMapping("/carbon/credit/list/{id}")
    @RequiresPermissions("cfd:neutrality:credit")
    public Result<List<ProCarbonCreditVo>> listCarbonCredit(@PathVariable("id") Long id) {
        checkPermWithCity(id);
        return success(iCfdCarbonNeutralityService.listCarbonCredit(id));
    }

    private void checkPermWithCity(Long id) {
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        if (cityId != null) {
            Long belongUser = iCfdCarbonNeutralityService.getByIdOrEx(id).getBelongUser();
            SysFrontUser frontUser = SpringContextHolder.getBean(ISysFrontUserService.class).getByIdOrEx(belongUser);
            if (!Objects.equals(cityId, frontUser.getCityId())) {
                throw new GlobalException(ResultCode.NOT_PERMISSION);
            }
        }
    }
}
